const arr = [
  { id: "a", pid: "", name: "总裁办" },
  { id: "b", pid: "", name: "行政办" },
  { id: "c", pid: "", name: "财务部" },
  { id: "d", pid: "c", name: "财务核算部" },
  { id: "e", pid: "c", name: "税务管理部" },
];

function fn1(list, rootValue) {
  const arr = [];
  list.forEach((item) => {
    if (item.id === rootValue) {
      const children = fn2(list, item.id);
      if (children.length) {
        item.children = children;
      }
      arr.push(item);
    }
  });
  return arr;
}

fn1(arr,'')










function fn2(list, rootValue) {
  const arr = [];
  list.forEach((item) => {
    if (item.id === rootValue) {
      const children = fn3(list, item.id);
      if (children.length) {
        item.children = children;
      }
      arr.push(item);
    }
  });
  return arr;
}

function fn3(list, rootValue) {
  const arr = [];
  list.forEach((item) => {
    if (item.id === rootValue) {
      const children = fn4(list, item.id);
      if (children.length) {
        item.children = children;
      }
      arr.push(item);
    }
  });
  return arr;
}
/* const r = tranListToTreeData(arr, "");
console.log(r); */
